"""
首页服务
"""

from typing import List, Dict, Any
from sqlalchemy.orm import Session
from datetime import datetime, timedelta

from database import Banner as BannerDB, News as NewsDB, Message as MessageDB

class HomeService:
    """首页服务类"""
    
    def __init__(self, db: Session):
        self.db = db
    
    async def get_home_data(self, user_id: int) -> Dict[str, Any]:
        """获取首页数据"""
        # 获取各种数据
        banners = await self.get_banners()
        announcements = await self.get_announcements(page=1, page_size=5)
        news = await self.get_news(page=1, page_size=5)
        
        # 快速操作
        quick_actions = [
            {
                "id": "scan",
                "title": "扫码",
                "icon": "qr_code_scanner",
                "route": "/scan",
                "color": "#2196F3"
            },
            {
                "id": "approval",
                "title": "审批",
                "icon": "approval",
                "route": "/approval",
                "color": "#4CAF50"
            },
            {
                "id": "report",
                "title": "报表",
                "icon": "assessment",
                "route": "/report",
                "color": "#FF9800"
            },
            {
                "id": "contact",
                "title": "通讯录",
                "icon": "contacts",
                "route": "/contact",
                "color": "#9C27B0"
            }
        ]
        
        # 统计数据
        statistics = {
            "pendingTasks": 5,
            "completedTasks": 23,
            "totalMessages": 12,
            "unreadMessages": 3
        }
        
        # 最近活动
        recent_activities = [
            {
                "id": 1,
                "type": "approval",
                "title": "请假申请已通过",
                "description": "您的年假申请已获得批准",
                "time": (datetime.now() - timedelta(hours=2)).isoformat(),
                "status": "approved"
            },
            {
                "id": 2,
                "type": "message",
                "title": "收到新消息",
                "description": "来自张经理的工作安排",
                "time": (datetime.now() - timedelta(hours=5)).isoformat(),
                "status": "unread"
            }
        ]
        
        return {
            "banners": banners,
            "announcements": announcements.get("data", []),
            "news": news.get("data", []),
            "quickActions": quick_actions,
            "statistics": statistics,
            "recentActivities": recent_activities
        }
    
    async def get_banners(self) -> List[Dict[str, Any]]:
        """获取轮播图"""
        # 模拟轮播图数据
        return [
            {
                "id": 1,
                "title": "企业文化建设",
                "image": "https://example.com/banner1.jpg",
                "url": "/news/1",
                "sort_order": 1
            },
            {
                "id": 2,
                "title": "新产品发布",
                "image": "https://example.com/banner2.jpg",
                "url": "/news/2",
                "sort_order": 2
            },
            {
                "id": 3,
                "title": "团队建设活动",
                "image": "https://example.com/banner3.jpg",
                "url": "/news/3",
                "sort_order": 3
            }
        ]
    
    async def get_news(self, page: int = 1, page_size: int = 10) -> Dict[str, Any]:
        """获取企业新闻"""
        # 模拟新闻数据
        news_list = [
            {
                "id": 1,
                "title": "公司荣获行业优秀企业奖",
                "summary": "在2024年度行业评选中，我公司凭借优异表现荣获优秀企业奖",
                "content": "详细内容...",
                "author": "企业宣传部",
                "publishTime": (datetime.now() - timedelta(days=1)).isoformat(),
                "readCount": 156,
                "category": "企业荣誉"
            },
            {
                "id": 2,
                "title": "新办公楼正式启用",
                "summary": "经过半年的建设，公司新办公楼今日正式启用",
                "content": "详细内容...",
                "author": "行政部",
                "publishTime": (datetime.now() - timedelta(days=3)).isoformat(),
                "readCount": 89,
                "category": "公司动态"
            }
        ]
        
        # 分页处理
        start = (page - 1) * page_size
        end = start + page_size
        
        return {
            "data": news_list[start:end],
            "total": len(news_list),
            "page": page,
            "page_size": page_size
        }
    
    async def get_announcements(self, page: int = 1, page_size: int = 10) -> Dict[str, Any]:
        """获取公告"""
        # 模拟公告数据
        announcements = [
            {
                "id": 1,
                "title": "系统维护通知",
                "content": "系统将于本周六晚上22:00-24:00进行维护升级，期间可能影响正常使用",
                "type": "system",
                "priority": "high",
                "publishTime": (datetime.now() - timedelta(hours=6)).isoformat(),
                "author": "系统管理员",
                "isTop": True
            },
            {
                "id": 2,
                "title": "新功能上线通知",
                "content": "移动端新增二维码扫描功能，支持设备管理和人员信息查询",
                "type": "feature",
                "priority": "medium",
                "publishTime": (datetime.now() - timedelta(days=1)).isoformat(),
                "author": "产品团队",
                "isTop": False
            }
        ]
        
        # 分页处理
        start = (page - 1) * page_size
        end = start + page_size
        
        return {
            "data": announcements[start:end],
            "total": len(announcements),
            "page": page,
            "page_size": page_size
        }